// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SUGGEST_TYPES_H_
#define SUGGEST_TYPES_H_

#include <cmath>

namespace suggest {

// Simple 2D math vector for locations and sizes
template<typename T> struct vec2 {
	T x;
	T y;

	vec2() {}
	vec2(T _x, T _y) : x(_x), y(_y) {}

	T abs_sq() {
		return x*x + y*y;
	}
	T abs() {
		return (T)sqrt(static_cast<double>(abs_sq()));
	}
};

template<typename T> vec2<T> add(vec2<T> a, vec2<T> b) {
	vec2<T> res;
	res.x = a.x + b.x;
	res.y = a.y + b.y;
	return res;
}

template<typename T> vec2<T> sub(vec2<T> a, vec2<T> b) {
	vec2<T> res;
	res.x = a.x - b.x;
	res.y = a.y - b.y;
	return res;
}

template<typename T> vec2<T> mul_elem(vec2<T> a, vec2<T> b) {
	vec2<T> res;
	res.x = a.x * b.x;
	res.y = a.y * b.y;
	return res;
}

template<typename T> vec2<T> div_elem(vec2<T> a, vec2<T> b) {
	vec2<T> res;
	res.x = a.x / b.x;
	res.y = a.y / b.y;
	return res;
}

template<typename T> vec2<T> mul(vec2<T> a, T b) {
	vec2<T> res;
	res.x = a.x * b;
	res.y = a.y * b;
	return res;
}

template<typename T> vec2<T> div(vec2<T> a, T b) {
	vec2<T> res;
	res.x = a.x / b;
	res.y = a.y / b;
	return res;
}

typedef vec2<int> vec2i;
typedef vec2<float> vec2f;

template<typename T> struct rect2 {
	vec2<T> pos;
	vec2<T> size;

	rect2() {}
	rect2(vec2<T> pos, vec2<T> size) : pos(pos), size(size) {}

	vec2<T> center() const {
		return add(pos, mul(size, 0.5f));
	}

	rect2<T> resized(vec2<T> new_size) const {
		return rect2<T>(sub(center(), mul(new_size, 0.5f)), new_size);
	}

	bool intersects(rect2 other) const {
		vec2<T> delta = sub(other.pos, pos);
		return delta.x < size.x && delta.x > -other.size.x &&
			   delta.y < size.y && delta.y > -other.size.y;
	}

	bool contains(vec2<T> point) const {
		return point.x > pos.x && point.x < pos.x + size.x &&
		       point.x > pos.y && point.y < pos.y + size.y;
	}
};

typedef rect2<int> rect2i;
typedef rect2<float> rect2f;

typedef int charcode;

}  // namespace suggest

#endif  // SUGGEST_TYPES_H_